import tensorflow as tf
def sparsemax_call():
    #batch_size = 2
    labels = tf.constant([[0, 0, 0, 1],[0, 1, 0, 0]])
    logits = tf.constant([[-3.4, 2.5, -1.2, 5.5],[-3.4, 2.5, -1.2, 5.5]])

    loss = tf.nn.softmax_cross_entropy_with_logits(labels=labels, logits=logits)
    loss_s = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.argmax(labels,1), logits=logits)
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.7)#设置每个GPU应该拿出多少容量给进程使用，0.4代表 40%
    config = tf.ConfigProto(gpu_options=gpu_options)
    session = tf.Session(config=config)
    # sess = tf.compat.v1.Session()#兼容性
    # with tf.compat.v1.Session() as sess:
    print("softmax loss:", session.run(loss))
    print ("sparse softmax loss:", session.run(loss_s))
sparsemax_call()

def check_tf_gpu():
    from tensorflow.python.client import device_lib
    print(tf.test.is_gpu_available())#查看gpu版本是否可用
    print(device_lib.list_local_devices())#输出装置名称
    print("Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
# check_tf_gpu()